package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	fmt.Println("Starting application manager server...")

	// 注册路由处理器
	http.HandleFunc("/", homeHandler)
	http.HandleFunc("/update", updateHandler)
	http.HandleFunc("/upload", uploadHandler)
	http.HandleFunc("/uploadJar", uploadJarHandler)
	http.HandleFunc("/stop", stopHandler)
	http.HandleFunc("/start", startHandler)
	http.HandleFunc("/restart", restartHandler)
	http.HandleFunc("/uploadFrontend", uploadFrontendHandler)
	http.HandleFunc("/restartNginx", restartNginxHandler)
	http.HandleFunc("/reloadNginx", reloadNginxHandler)
	http.HandleFunc("/viewLog", viewLogHandler)
	http.HandleFunc("/downloadLog", downloadLogHandler)

	// Nginx 配置管理相关路由（需要认证）
	http.HandleFunc("/nginxConfig/login", nginxConfigLoginHandler)
	http.HandleFunc("/nginxConfig/logout", nginxConfigLogoutHandler)
	http.HandleFunc("/nginxConfig", nginxConfigAuthMiddleware(nginxConfigHandler))
	http.HandleFunc("/nginxConfig/read", nginxConfigAuthMiddleware(nginxConfigReadHandler))
	http.HandleFunc("/nginxConfig/update", nginxConfigAuthMiddleware(nginxConfigUpdateHandler))
	http.HandleFunc("/nginxConfig/create", nginxConfigAuthMiddleware(nginxConfigCreateHandler))
	http.HandleFunc("/nginxConfig/createDir", nginxConfigAuthMiddleware(nginxConfigCreateDirHandler))
	http.HandleFunc("/nginxConfig/delete", nginxConfigAuthMiddleware(nginxConfigDeleteHandler))
	http.HandleFunc("/nginxConfig/deleteDir", nginxConfigAuthMiddleware(nginxConfigDeleteDirHandler))
	http.HandleFunc("/nginxConfig/rename", nginxConfigAuthMiddleware(nginxConfigRenameHandler))
	http.HandleFunc("/nginxConfig/test", nginxConfigAuthMiddleware(nginxConfigTestHandler))

	// 启动服务器
	log.Println("Server started on :8765")
	err := http.ListenAndServe(":8765", nil)
	if err != nil {
		log.Fatal("Server failed to start: ", err)
	}
}
